﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MapEditor.Tiles;
using MapEditor.Tools;

namespace MapEditor
{
    class DisplayDrawer
    {
        private readonly Color displayClearColor = new Color(240, 240, 240);

        public readonly SpriteBatch spriteBatch;
        private readonly Selection selectorTileset;

        public DisplayDrawer(SpriteBatch spriteBatch, Selection selectorTileset)
        {
            this.spriteBatch = spriteBatch;
            this.selectorTileset = selectorTileset;
        }

        public void DrawMap(Map map, Tool currentTool, Rectangle toolPreviewRect, Rectangle toolPreviewRectNoGrid, Rectangle viewingRectangle, TileType tileType, bool showGrid, bool renderDisplay, int type, bool isQuad)
        {
            Vector3 previewCamera = new Vector3(-viewingRectangle.X, -viewingRectangle.Y, 0f);

            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null, Matrix.CreateTranslation(previewCamera));

            spriteBatch.GraphicsDevice.Clear(displayClearColor);

            if (renderDisplay)
            {
                Rectangle currentViewingRectangle = new Rectangle(viewingRectangle.X / map.TileSize, viewingRectangle.Y / map.TileSize, viewingRectangle.Width / map.TileSize + 3, viewingRectangle.Height / map.TileSize + 3); // Draw a couple more just in case

               
                    if (showGrid)
                    {
                        currentTool.DrawRegularTilePreview(spriteBatch, map.ActiveLayer.texture[type], toolPreviewRect);
                        if (toolPreviewRect != Rectangle.Empty)
                            Utility.DrawRectangle(spriteBatch, 3, toolPreviewRect, Color.Orange);
                    }
                    else
                    {
                        currentTool.DrawRegularTilePreview(spriteBatch, map.ActiveLayer.texture[type], toolPreviewRectNoGrid);
                    }

                    if (isQuad)
                        map.ActiveLayer.DrawQuadTree(spriteBatch);

                map.ActiveLayer.Draw(spriteBatch, toolPreviewRect, currentViewingRectangle); // Draws the layer and skips tiles covered by the tool preview

                map.ActiveLayer.DrawNonTile(spriteBatch, toolPreviewRect, currentViewingRectangle);

                
                

               

                 // Draw border around selector
                //DrawLayersWithToolPreviews(map, currentTool, toolPreviewRect, toolPreviewRectNoGrid, viewingRectangle, tileType, showOtherLayers, showCollisionLayer, type);

                if (showGrid)
                    DrawGrid(map.TileSize, viewingRectangle);
            }

            spriteBatch.End();
        }


        public void DrawTileset(Map map, Rectangle viewingRectangle, bool showGrid, bool renderDisplay)
        {
            Vector3 tilesetCamera = new Vector3(-viewingRectangle.X, -viewingRectangle.Y, 0f);

            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, Matrix.CreateTranslation(tilesetCamera));

            spriteBatch.GraphicsDevice.Clear(displayClearColor);

            if (renderDisplay)
            {
                spriteBatch.Draw(map.TilesetTexture, Vector2.Zero, Color.White);

                if (showGrid)
                    DrawGrid(map.TileSize, viewingRectangle);

                Utility.DrawRectangle(spriteBatch, 3, selectorTileset.Rectangle, Color.Red); // Tileset selector border
            }

            spriteBatch.End();
        }

        private void DrawGrid(int tileSize, Rectangle viewingRectangle)
        {
            for (int x = viewingRectangle.X; x < viewingRectangle.Width + viewingRectangle.X; x += tileSize)
                for (int y = viewingRectangle.Y; y < viewingRectangle.Height + viewingRectangle.Y; y += tileSize)
                    Utility.DrawRectangle(spriteBatch, 1, new Rectangle(x / tileSize * tileSize, y / tileSize * tileSize, tileSize, tileSize), Color.Black);
        }

        private void DrawLayersWithToolPreviews(Map map, Tool currentTool, Rectangle toolPreviewRect, Rectangle toolPreviewRectNoGrid, Rectangle viewingRectangle, TileType tileType, bool showOtherLayers, bool showCollisionLayer, int type)
        {
            
        }
    }
}
